/*
 * Sesame Inc.
 * Copyright (c) 2009 All Rights Reserved.
 */
package com.sesame.aop;

import java.util.Date;

import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;

import com.sesame.dao.AdminDAO;
import com.sesame.dao.AdminLogDAO;
import com.sesame.dao.NoticeDAO;
import com.sesame.domain.Admin;
import com.sesame.domain.AdminLog;
import com.sesame.domain.Notice;

/**
 * @author voidman
 * 
 */
@Aspect
public class AdminLoggerAspect {

    private Admin admin;
    private AdminLog adminLog;
    private String content;
    private AdminLogDAO adminLogDAO;
    private AdminDAO adminDAO;
    private NoticeDAO noticeDAO;

    public void setAdmin(Admin admin) {
        this.admin = admin;
    }

    public void setAdminLog(AdminLog adminLog) {
        this.adminLog = adminLog;
    }

    public void setAdminLogDAO(AdminLogDAO adminLogDAO) {
        this.adminLogDAO = adminLogDAO;
    }

    public void setAdminDAO(AdminDAO adminDAO) {
        this.adminDAO = adminDAO;
    }

    public void setNoticeDAO(NoticeDAO noticeDAO) {
        this.noticeDAO = noticeDAO;
    }

    public void addLog() {
        Date createTime = new Date();
        adminLog.setCreateTime(createTime);
        adminLog.setContent(content);
        adminLog.setAdminId(admin.getId());
        adminLog.setUserName(admin.getUserName());

        adminLogDAO.addAdminLog(adminLog);

    }

    @AfterReturning(value = ("execution(* com.sesame.serviceimpl.AdminServiceImpl.isLogin(..)) && args(*,userName,..)"), returning = "myReturn")
    public void login(boolean myReturn, String userName) {

        if (myReturn) {
            content = "成功登录后台";
            admin = adminDAO.getAdminByUserName(userName);
            adminLog.setAdminId(admin.getId());
            adminLog.setUserName(userName);

        } else {
            content = userName + "登录后台失败";
            adminLog.setAdminId(0);
            adminLog.setUserName("系统提示");
        }

        Date createTime = new Date();
        adminLog.setCreateTime(createTime);
        adminLog.setContent(content);
        adminLogDAO.addAdminLog(adminLog);

    }

    @AfterReturning(value = ("execution(* com.sesame.serviceimpl.AdminServiceImpl.isNewAdmin(..))&& args(newAdmin,..)"), returning = "myReturn")
    public void newAdmin(Admin newAdmin, boolean myReturn) {

        if (myReturn) {
            content = "成功增加普通管理员" + newAdmin.getUserName();

        } else {
            content = "增加普通管理员失败";

        }

        addLog();

    }

    @Before(value = ("execution(* com.sesame.serviceimpl.AdminServiceImpl.deleteAdmin(..))&& args(id)"))
    public void deleteAdmin(Integer id) {

        Admin refAdmin = adminDAO.getAdminById(id);
        content = "成功删除普通管理员" + refAdmin.getUserName();

        addLog();

    }

    @AfterReturning(value = ("execution(* com.sesame.serviceimpl.AdminServiceImpl.resetPassword(..))&& args(id)"))
    public void resetPassword(Integer id) {

        Admin refAdmin = adminDAO.getAdminById(id);
        content = "成功重置普通管理员" + refAdmin.getUserName() + "的密码为111111";

        addLog();

    }

    @AfterReturning(value = ("execution(* com.sesame.serviceimpl.AdminServiceImpl.isNewNotice(..))&& args(newNotice,..)"), returning = "myReturn")
    public void isNewNotice(Notice newNotice, boolean myReturn) {

        if (myReturn) {
            content = "成功添加公告" + newNotice.getTitle();
        } else {
            content = "添加公告失败";
        }

        addLog();

    }

    @AfterReturning(value = ("execution(* com.sesame.serviceimpl.AdminServiceImpl.isEditNotice(..))&& args(refNotice,..)"), returning = "myReturn")
    public void isEditNotice(Notice refNotice, boolean myReturn) {

        if (myReturn) {
            content = "成功修改公告" + refNotice.getTitle();
        } else {
            content = "修改公告失败";
        }

        addLog();

    }

    @Before(value = ("execution(* com.sesame.serviceimpl.AdminServiceImpl.deleteNotice(..))&& args(id)"))
    public void deleteNotice(Integer id) {

        Notice refNotice = noticeDAO.getNoticeById(id);
        content = "成功删除公告" + refNotice.getTitle();

        addLog();

    }

    @AfterReturning(value = ("execution(* com.sesame.serviceimpl.AdminServiceImpl.modifyPassword(..))"), returning = "myReturn")
    public void modifyPassword(boolean myReturn) {

        if (myReturn) {
            content = "成功修改密码";
        } else {
            content = "修改密码失败";
        }

        addLog();

    }

}